<?php
namespace Helpers;

use Phalcon\Mvc\View;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\Controller as PhalconController;

class Controller extends PhalconController
{

    protected static $module = 'admin';
//    const moduleName = '基础控制器';
//    public $log;

    /**
     * 初始化日志（后于控制器内initialize()方法）
     *
     * create by ck 20180307
     */
    public function onConstruct()
    {
//        $this->log = new \stdClass();
//        $this->log->
    }

    public function afterExecuteRoute(Dispatcher $dispatcher)
    {
        if (!$this->view->isDisabled()) {
            return $this->view->setVar('Ver', '?ver=' . VER);
        }
        return true;
    }

    public function createCommitLog($msg, $detail = '')
    {
        return Main::createCommitLog(
            self::$module,
            $this->dispatcher->getControllerName(),
            $this->dispatcher->getActionName(),
            [
                'post' => $this->request->getPost(),
                'get' => $this->request->getQuery(),
                'body' => $this->request->getRawBody(),
                'detail' => $detail
            ],
            null,
            $msg
        );
    }

    /**
     * 返回错误
     *
     * @param int $code         http状态码
     * @param string $error     错误
     * @param string $msg       错误详细信息
     * @param bool $log         是否记录日志
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20180203
     */
    public function sendErrorResponse($code = 200, $error = '未知错误', $msg = '', $log = true)
    {
        $content = ['error' => $error, 'code' => $code, 'msg' => $msg];
//        $msg and $content['msg'] = $msg;
        $log and $this->createCommitLog($error, $msg);
        return $this->response->setStatusCode($code)->setJsonContent($content, JSON_UNESCAPED_UNICODE);
    }

    /**
     * 返回成功
     *
     * @param string $msg       成功消息
     * @param bool $log         是否记录日志
     * @param int $code         http状态码
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20180203
     */
    public function sendSuccessResponse($msg = '', $log = true, $code = 200)
    {
        $log and $this->createCommitLog($msg);
        return $this->response->setJsonContent(
            ['code' => $code ?: 200, 'msg' => $msg ?: '成功！'],
            JSON_UNESCAPED_UNICODE
        );
    }

    /**
     * 返回成功消息体
     *
     * @param null $data
     * @param bool $log
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20180203
     */
    public function sendJsonResponse($data = null, $log = false)
    {
        $log and $this->createCommitLog('json返回成功', $data);
        return $this->response->setJsonContent($data, JSON_UNESCAPED_UNICODE);
    }

    public function sendDataGridError($error, $msg = null, $log = true)
    {
        $content = ['error' => $error, 'total' => 0, 'rows' => []];
        $msg and $content['msg'] = $msg;
        $log and $this->createCommitLog($error, $msg);
        return $this->response->setJsonContent($content, JSON_UNESCAPED_UNICODE);
    }

    public function sendViewError($error)
    {
        $this->view->disableLevel([
            View::LEVEL_LAYOUT,
            View::LEVEL_BEFORE_TEMPLATE,
            View::LEVEL_AFTER_TEMPLATE,
            View::LEVEL_ACTION_VIEW,
        ]);
        return $this->flash->error($error);
    }

    public function sendViewSuccess($msg)
    {
        $this->view->disableLevel([
            View::LEVEL_LAYOUT,
            View::LEVEL_BEFORE_TEMPLATE,
            View::LEVEL_AFTER_TEMPLATE,
            View::LEVEL_ACTION_VIEW,
        ]);
        return $this->flash->success($msg);
    }

}